package cn.wikiflyer.beikang.act.user;

import java.lang.reflect.Type;

import kankan.wheel.widget.WheelView;
import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.user.bean.BKAddressResultBean;
import cn.wikiflyer.beikang.act.user.bean.AddressParams;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wikiflyer.beikang.utils.AddressUtil;
import cn.wk.libs4a.WKApplication;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.utils.L;
import cn.wk.libs4a.utils.WKUtils;
import cn.wk.libs4a.view.WKHeader;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 地址修改 and 地址添加
 * 
 * @author qunlee_mr
 * 
 */
public class UserAddressAAndUAct extends BaseActivity {

	@ViewInject(id = R.id.header)
	private WKHeader header;

	@ViewInject(id = R.id.addinfo_name)
	private EditText name;
	@ViewInject(id = R.id.addinfo_phone)
	private EditText phone;
	@ViewInject(id = R.id.addinfo_detail)
	private EditText detail;
	@ViewInject(id = R.id.addinfo_defcheck)
	private CheckBox checkDefault;

	@ViewInject(id = R.id.addinfo_probtn)
	private TextView probtn;
	@ViewInject(id = R.id.addinfo_citybtn)
	private TextView citybtn;
	@ViewInject(id = R.id.addinfo_areabtn)
	private TextView areabtn;
	// @ViewInject(id = R.id.addinfo_save, click = "onClick")
	// private Button savebtn;

	@ViewInject(id = R.id.selectcity, click = "onClick")
	private ImageButton select_city;
	@ViewInject(id = R.id.wheel_b, click = "onClick")
	private Button select_b;
	// @ViewInject(id = R.id.wheel_c, click = "onClick")
	// private Button select_c;
	@ViewInject(id = R.id.wheel_v, click = "onClick")
	private View wheel_v;

	@ViewInject(id = R.id.wheel_l)
	private LinearLayout wheel_l;
	@ViewInject(id = R.id.wheel_linear, click = "onClick")
	private LinearLayout wheel_linear;

	private AddressUtil mAddressUtil;

	private BKAddressResultBean bean;
	private boolean ifAdd;

	@Override
	public void getIntentData(Bundle savedInstanceState) {
		super.getIntentData(savedInstanceState);
		Intent intent = getIntent();
		bean = (BKAddressResultBean) intent.getSerializableExtra("addressbean");
		if (bean != null) {
			ifAdd = false;
		} else {
			bean = null;
			ifAdd = true;
		}
	}

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_user_address_aau);
	}

	@Override
	public void init() {
		mAddressUtil = new AddressUtil(ctx);
		header.setLeftImg("保存", R.drawable.user_add_save_img, false, 12);
		header.setClickListener(null, null, new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (ifAdd) { // 新增收货地址
					if (checkEmpty()) {
						new AddAddressTask(ctx);
					}
				} else {
					if (checkEmpty()) {
						new EditAddressTask(ctx); // 修改收货地址
					}
				}
			}
		});

		if (ifAdd) {
			header.setTitle("新增地址");
		} else {
			header.setTitle("修改地址");
		}
		mProvinceWheel = (WheelView) findViewById(R.id.id_province);
		mCityWheel = (WheelView) findViewById(R.id.id_city);
		mAreaWheel = (WheelView) findViewById(R.id.id_area);
		// initAddressDetail();
		mAddressUtil.initWheel(mProvinceWheel, mCityWheel, mAreaWheel);
		mAddressUtil.setInitData();
	}

	/**
	 * 检测信息是否填写完整 填写完整返回true
	 */
	private boolean checkEmpty() {
		if (name.getText().toString().isEmpty()) {
			Toast.makeText(ctx, "填写收货人", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (phone.getText().toString().isEmpty()) {
			Toast.makeText(ctx, "填写手机号", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (!WKUtils.isTel(phone.getText().toString())) {
			Toast.makeText(ctx, "手机号格式不正确", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (detail.getText().toString().isEmpty()) {
			Toast.makeText(ctx, "填写详细地址", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	@Override
	public void setData() {
		if (!ifAdd) { // 编辑
			name.setText(bean.receiver);
			phone.setText(bean.phone);
			detail.setText(bean.address);
			mAddressUtil.setUserData(bean.sheng, bean.shi, bean.qu);
			probtn.setText(mAddressUtil.getProvince().regionName);
			citybtn.setText(mAddressUtil.getCity().regionName);
			if (mAddressUtil.getArea() == null) {
				areabtn.setText("");
			} else {
				areabtn.setText(mAddressUtil.getArea().regionName);
			}
			checkDefault.setChecked(bean.default_add == 1 ? true : false);
		} else {
		}
	}

	boolean iffirst = true;

	public void onClick(View v) {
		if (v.getId() == R.id.selectcity || v.getId() == R.id.wheel_linear) { // 弹出城市选择框
			if (iffirst) {
				iffirst = false;
			}
			wheel_l.setVisibility(View.VISIBLE);
			wheel_v.setVisibility(View.VISIBLE);
			select_city.setImageResource(R.drawable.city_selected);
		} else if (v.getId() == R.id.wheel_b) { // 确定按钮
			wheel_l.setVisibility(View.GONE);
			wheel_v.setVisibility(View.GONE);
			select_city.setImageResource(R.drawable.city_select);
			probtn.setText(mAddressUtil.getProvince().regionName);
			citybtn.setText(mAddressUtil.getCity().regionName);
			if (mAddressUtil.getArea() == null
					|| mAddressUtil.getArea().regionName == null) {
				areabtn.setText("");
			} else {
				areabtn.setText(mAddressUtil.getArea().regionName);
			}
		} else if (v.getId() == R.id.wheel_v) {

		}
	}

	/**
	 * 省的WheelView控件
	 */
	private WheelView mProvinceWheel;
	/**
	 * 市的WheelView控件
	 */
	private WheelView mCityWheel;
	/**
	 * 区的WheelView控件
	 */
	private WheelView mAreaWheel;

	// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	/**
	 * 添加收货地址
	 */
	class AddAddressTask extends WKAsyncTaskPro {
		public AddAddressTask(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... params) {
			AddressParams addParams = new AddressParams();
			addParams.ed_sheng = mAddressUtil.getProvince().regionId;
			addParams.ed_shi = mAddressUtil.getCity().regionId;
			if (mAddressUtil.getArea() == null) {
				addParams.ed_qu = "0";
			} else {
				addParams.ed_qu = mAddressUtil.getArea().regionId;
			}
			addParams.ed_address = detail.getText().toString();
			addParams.ed_phone = phone.getText().toString();
			addParams.ed_name = name.getText().toString();
			addParams.default_add = checkDefault.isChecked() ? 1 : 0;
			return app().net.addAddress(addParams.ed_sheng, addParams.ed_shi,
					addParams.ed_qu, addParams.ed_address, addParams.ed_phone,
					addParams.ed_name, addParams.default_add);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				WKApplication.showToast("添加地址成功.");
				activity.finish();
			} else {
				WKApplication.showToast("添加地址失败." + base.info);
			}
		}
	}

	/**
	 * 修改收货地址
	 */
	class EditAddressTask extends WKAsyncTaskPro {
		public EditAddressTask(Context context) {
			super(context);
		}

		@Override
		protected String doInBackground(boolean... params) {
			// (String addid, String sheng, String shi, String qu, String
			// address, String phone, String receiver, int default_add
			AddressParams addParams = new AddressParams();
			addParams.ed_addid = bean.id;
			addParams.ed_sheng = mAddressUtil.getProvince().regionId;
			addParams.ed_shi = mAddressUtil.getCity().regionId;
			if (mAddressUtil.getArea() == null) {
				addParams.ed_qu = "";
			} else {
				addParams.ed_qu = mAddressUtil.getArea().regionId;
			}
			addParams.ed_address = detail.getText().toString();
			addParams.ed_phone = phone.getText().toString();
			addParams.ed_name = name.getText().toString();
			addParams.default_add = checkDefault.isChecked() ? 1 : 0;
			L.wj(addParams.toString());
			return app().net.editAddress(addParams.ed_addid,
					addParams.ed_sheng, addParams.ed_shi, addParams.ed_qu,
					addParams.ed_address, addParams.ed_phone,
					addParams.ed_name, addParams.default_add);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				WKApplication.showToast("修改地址成功.");
				// Intent intent = new Intent(ctx, UserAddressInfoAct.class);
				// intent.putExtra("name", name.getText().toString());
				// intent.putExtra("phone", phone.getText().toString());
				// intent.putExtra("province", probtn.getText().toString());
				// intent.putExtra("city", citybtn.getText().toString());
				// intent.putExtra("area", areabtn.getText().toString());
				// intent.putExtra("detail", detail.getText().toString());
				// setResult(0, intent);
				activity.finish();
			} else {
				WKApplication.showToast("修改地址失败." + base.info);
			}
		}
	}
}
